/**
 * 
 */
package utilities;

import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;

/**
 * If the logger is active send the logger messages to the 
 * logger file. <br /> 
 * <i>This does not check to see if a logger file has already 
 * been created. This is assumed that the users will understand 
 * that messages are meant not to be displayed, and if messages 
 * are displayed then the log file will be overwritten each time 
 * the logger is access the same file. </i>
 * <br /><br />
 * This will only send messages or errors if 
 * there are any errors or messages present. The logger 
 * will also enumerate each message that occurs.
 * <br /><br />
 * The default format for the logger will be errors 
 * are displayed then the messages are displayed in 
 * the following format:   
 * <br /><br /><code>
 * **********************<br />
 * *****   ERRORS   *****<br />
 * **********************<br />
 * 1: Error Message <i>1</i><br />
 * ...<br />
 * <i>n</i>: Error Message <i>n</i><br />
 * <br />
 * ************************<br />
 * *****   MESSAGES   *****<br />
 * ************************<br />
 * 1: Message <i>1</i><br />
 * ...<br />
 * <i>n</i>: Message <i>n</i></code><br />
 *  
 * @author Guillermo Mendez-Kestler
 * @since Rev 24
 */
public class Logger {
	/**
	 * Default filename for the active logger. Set to {@value}. 
	 */
	public static final String DEFAULT_FILE = "logger.txt";

	/**
	 * Sets if the logger is active or not. 
	 */
	private boolean active;

	/**
	 * The log file. 
	 */
	private File loggerFile;

	/**
	 * Printwriter that will write all the messages to 
	 * the log file.  
	 */
	private PrintWriter printer;

	/**
	 * List of messages. 
	 */
	private ArrayList<String> messages;

	/**
	 * List of errors. 
	 */
	private ArrayList<String> errors;

	/**
	 * Creates a logger using the default filename. The 
	 * current default file is placed in the root directory,
	 * with the filename of {@value DEFAULT_FILE}. 
	 * 
	 * @param verbose Sets the logger as active if {@code
	 * true}. 
	 */
	public Logger(boolean verbose) {
		if (verbose){
			active = verbose;
			loggerFile = new File(DEFAULT_FILE);
			messages = new ArrayList<String>();
			errors = new ArrayList<String>();
		}
	}

	/**
	 * Creates the active logger using the given filename. 
	 * 
	 * @param verbose Sets the logger as active if {@code
	 * true}. 
	 * @param fileName The file that will be created with 
	 * the logger. 
	 */
	public Logger(boolean verbose, File fileName) {
		if (verbose){
			active = verbose;
			loggerFile = fileName;
			messages = new ArrayList<String>();
			errors = new ArrayList<String>();
		}
	}

	/**
	 * Creates the active logger using the given filename. 
	 * 
	 * @param verbose Sets the logger as active if {@code
	 * true}. 
	 * @param fileName Creates a logger with the given 
	 * filename in the root directory. 
	 */
	public Logger(boolean verbose, String fileName) {
		if (verbose){
			active = verbose;
			loggerFile = new File(fileName);
			messages = new ArrayList<String>();
			errors = new ArrayList<String>();
		}
	}

	/**
	 * If the logger is active write the message to the logger. 
	 * 
	 * @param message The message to be written to the logger. 
	 * @param error {@code True} if the message is an error. 
	 */
	public void message(String message, boolean error) {
		if (active){
			if(error) {
				errors.add(message);
			}
			else if(active) {
				messages.add(message);
			}
		}
	}

	/**
	 * If the logger is active, begin sending the logger 
	 * messages to the logger file. This will only send 
	 * messages or errors if there are any errors or 
	 * messages present.  The logger will also enumerate 
	 * each message that occurs. 
	 */
	public void close(){
		if (active){
			try {
				printer = new PrintWriter(loggerFile);
			} catch (Exception e) {
				e.printStackTrace();
			}
			int i;

			if (!errors.isEmpty()){
				// PRINTING ERROR MESSAGES TO THE LOGGER FILE
				printer.println("**********************\n" +
						"*****   ERRORS   *****\n" +
				"**********************");
				printer.flush();
				i = 1;
				for (String msg:errors){
					printer.println(i+": "+msg);
					printer.flush();
					i++;
				}
			}

			if (!messages.isEmpty()){
				// PRINTING NORMAL MESSAGES TO THE LOGGER FILE
				printer.println("\n************************\n" +
						"*****   MESSAGES   *****\n" +
				"************************");
				printer.flush();
				i=1; // Reset line counter
				for (String msg:errors){
					printer.println(i+": "+msg);
					printer.flush();
					i++;
				}
			}
			// TERMINATING THE FILE
			printer.flush();
			printer.close();
		}
	}
}
